///
//  HMHomeViewController.m
//  G05爱鲜蜂
//
//  Created by 源 何 on 16/10/15.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMHomeViewController.h"
#import "HMNewFeatureController.h"

#import "HMHomeModel.h"
#import "HMHomeHeadView.h"
#import "NetWorkTool.h"
#import <MJRefresh.h>

static NSString *reuseIdentifier = @"cc";


#import "DSHTTPClient.h"

@interface HMHomeViewController ()

@property (nonatomic, strong) UIView *coverView;


@property (nonatomic, strong) NSArray *iconsArray;

@property (nonatomic, strong) NSArray<HMHomeModel *> *activitiesArray;

@property (nonatomic, strong) NSDictionary *dataDict;

@end

@implementation HMHomeViewController

- (NSDictionary *)dataDict{
    if (nil == _dataDict) {
        _dataDict = [NSDictionary dictionary];
    }
    return _dataDict;
}

//cell图片
- (void)setActivitiesArray:(NSArray *)activitiesArray{
    _activitiesArray = activitiesArray;
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];


}

- (void)loadView{
    [super loadView];

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [HMHomeModel downloadWithKey:@"activities" andSuccessBlock:^(NSArray *array) {
        self.activitiesArray = array;
        //        NSLog(@"cell数据获取成功");
    } andFailureBlock:^{
        NSLog(@"cell获取失败");
    }];
    
    //加载导航栏
    [self setupNav];
    self.tableView.rowHeight = 160;

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifier];
    //设置头view
    HMHomeHeadView *headView = [[HMHomeHeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3)];
    
    headView.iconArray = self.iconsArray;
    
    self.tableView.tableHeaderView = headView;
    
//    NSLog(@"%@",NSStringFromCGRect(headView.frame));
//    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(headView.bounds.size.height, 0, 0, 0);
    self.tableView.showsVerticalScrollIndicator = NO;
    
    //设置尾view
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData:)];
    UIImage *image1 = [UIImage imageNamed:@"v2_pullRefresh1"];
    UIImage *image2 = [UIImage imageNamed:@"v2_pullRefresh2"];
    [header setImages:@[image1] forState:MJRefreshStateIdle];
    [header setImages:@[image2] forState:MJRefreshStatePulling];
    [header setImages:@[image1,image2] forState:MJRefreshStateRefreshing];
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松手开始刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    header.lastUpdatedTimeLabel.hidden = NO;
    self.tableView.mj_header = header;
}

- (void)refreshData:(MJRefreshGifHeader *)header{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
}

#pragma mark
#pragma mark -- tableView delegate dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.activitiesArray.count ? self.activitiesArray.count : 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];

    HMHomeModel *model = self.activitiesArray[indexPath.row];
    NSString *imageStr = model.img;
    NSURL *url = [NSURL URLWithString:imageStr];
    
//    NSLog(@"tableView --> url: %@",url);
    
    UIImageView *cellImage = [[UIImageView alloc] init];
    [cellImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"v2_placeholder_full_size"]];
    
    [cell.contentView addSubview:cellImage];
    
    [cellImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell.contentView);
    }];

    return cell;
}


#pragma mark
#pragma mark -- 设置导航条
- (void)setupNav{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBackImage"] forBarMetrics:UIBarMetricsDefault];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftBtn = [UIButton ff_buttonWithTitle:@"扫一扫" imageName:@"icon_black_scancode" target:self action:@selector(showCarmare)];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:9];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(-8, -10, 8, 10);
    leftBtn.titleEdgeInsets = UIEdgeInsetsMake(10, -35, -12, 35);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton *rightBtn = [UIButton ff_buttonWithTitle:@"搜索" imageName:@"icon_search" target:self action:@selector(showCarmare)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:9];
    rightBtn.imageEdgeInsets = UIEdgeInsetsMake(-8, 28, 8, -28);
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(10, 6, -12, -6);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

#pragma mark
#pragma mark -- 二维码 // 搜索
- (void)showCarmare{
    NSLog(@"点击了");
}

#pragma mark
#pragma mark -- 判断新旧版本
- (void)isNewVersion{
    
    // 获取当前的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    // 获取旧版本号
    NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kVersion1];

    if ([oldVersion isEqualToString:currentVersion]) {
        
    } else {
        // 显示欢迎界面
        [self setupCoverView];
        
        // 显示完一次欢迎界面之后, 应该保存当前的版本号
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:kVersion1];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

#pragma mark
#pragma mark -- 加载遮罩
- (void)setupCoverView{

    _coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    _coverView.backgroundColor = [UIColor colorWithRed:222/255 green:222/255 blue:222/255 alpha:1.0];
    _coverView.alpha = 0.5;
    [self.view addSubview:_coverView];
    
    UIImageView *coverImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"homepage_guide"]];
    coverImg.backgroundColor = [UIColor clearColor];
    [_coverView addSubview:coverImg];
    
    [coverImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.8);
        make.height.equalTo(self.view).multipliedBy(0.33);
    }];
    
    UIButton *removeBtn = [[UIButton alloc] init];
    [removeBtn setImage:[UIImage imageNamed:@"homepage_knownbtn"] forState:UIControlStateNormal];
    [_coverView addSubview:removeBtn];
    
    [removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).multipliedBy(1.65);
    }];
    
    [removeBtn addTarget:self action:@selector(removeAllAbove) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark
#pragma mark -- 移除遮罩
- (void)removeAllAbove{
    [_coverView removeFromSuperview];
}

@end
